package com.example.demo.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.example.demo.constant.SystemConstant;
import com.example.demo.constant.TaskConstant;
import com.example.demo.controller.bean.Result;
import com.example.demo.dto.CouponsDetailDTO;
import com.example.demo.dto.CouponsDetailRedemptionDTO;
import com.example.demo.dto.CouponsListDTO;
import com.example.demo.mapper.entity.Coupons;
import com.example.demo.mapper.entity.StudentCoupons;
import com.example.demo.service.StudentCouponsService;
import com.example.demo.utils.CouponsUtil;
import com.example.demo.utils.PageBo;
import com.example.demo.utils.PageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Lucy
 * @create 2024-02-21 20:50
 */
@RequestMapping("myCoupons")
@Api(value = "我的优惠券", tags = {"我的优惠券接口"})
@RestController
@Slf4j
public class MyCouponsController {

    @Autowired
    private StudentCouponsService couponsService;

    @ApiOperation("获取全部可用优惠券列表")
    @RequestMapping(value = "getActiveCouponsList", method = {RequestMethod.POST})
    public Result<PageVo<CouponsListDTO>> getActiveCouponsList(@RequestBody PageBo pageBo){
        int studentId = StpUtil.getLoginIdAsInt();
        PageVo<CouponsListDTO> activeCouponsList =  couponsService.getStatusCouponsList(pageBo, studentId, TaskConstant.COUPONS_STATUS_ACTIVE);
        return Result.ok(activeCouponsList);
    }

    @ApiOperation("获取最新获得优惠券列表")
    @RequestMapping(value = "getNewlyAcquiredCouponsList", method = {RequestMethod.POST})
    public Result<PageVo<CouponsListDTO>> getNewlyAcquiredCouponsList(@RequestBody PageBo pageBo){
        int studentId = StpUtil.getLoginIdAsInt();
        PageVo<CouponsListDTO> newlyAcquiredCouponsList =  couponsService.getNewlyAcquiredCouponsList(pageBo, studentId, TaskConstant.COUPONS_STATUS_ACTIVE);
        return Result.ok(newlyAcquiredCouponsList);
    }

    @ApiOperation("获取即将过期优惠券列表")
    @RequestMapping(value = "getExpiringCouponsList", method = {RequestMethod.POST})
    public Result<PageVo<CouponsListDTO>> getExpiringCouponsList(@RequestBody PageBo pageBo){
        int studentId = StpUtil.getLoginIdAsInt();
        PageVo<CouponsListDTO> expiringCouponsList =  couponsService.getExpiringCouponsList(pageBo, studentId, TaskConstant.COUPONS_STATUS_ACTIVE);
        return Result.ok(expiringCouponsList);
    }

    @ApiOperation("获取已使用优惠券列表")
    @RequestMapping(value = "getUsedCouponsList", method = {RequestMethod.POST})
    public Result<PageVo<CouponsListDTO>> getUsedCouponsList(@RequestBody PageBo pageBo){
        int studentId = StpUtil.getLoginIdAsInt();
        PageVo<CouponsListDTO> usedCouponsList =  couponsService.getStatusCouponsList(pageBo, studentId, TaskConstant.COUPONS_STATUS_USED);
        return Result.ok(usedCouponsList);
    }

    @ApiOperation("获取已失效优惠券列表")
    @RequestMapping(value = "getExpiredCouponsList", method = {RequestMethod.POST})
    public Result<PageVo<CouponsListDTO>> getExpiredCouponsList(@RequestBody PageBo pageBo){
        int studentId = StpUtil.getLoginIdAsInt();
        PageVo<CouponsListDTO> expiredCouponsList =  couponsService.getStatusCouponsList(pageBo, studentId, TaskConstant.COUPONS_STATUS_EXPIRED);
        return Result.ok(expiredCouponsList);
    }
    //用于代金券、满减券和折扣券的获取优惠券详情
    @ApiOperation("获取优惠券详情接口")
    @GetMapping(value = "/getCouponsDetail/{couponsNumber}")
    public Result<CouponsDetailDTO> getCouponsDetail(@PathVariable String couponsNumber){
        StudentCoupons studentCoupons = couponsService.selectStudentCouponsByNumber(couponsNumber);
        Coupons coupons = couponsService.selectCouponsById(studentCoupons.getCouponsId());
        CouponsDetailDTO dto = new CouponsDetailDTO();
        dto.setCouponsId(coupons.getCouponsId());
        dto.setCouponsNumber(studentCoupons.getCouponsNumber());
        dto.setCouponsLabel(CouponsUtil.generateCouponLabel(coupons));
        dto.setStartDate(coupons.getStartDate());
        dto.setEndDate(coupons.getEndDate());
        dto.setStoreAddress(couponsService.selectAddressById(coupons.getAdvertiserId()));
        dto.setActivityDetails("1."+ coupons.getDescription() + "\n2." + coupons.getCondition());
        return Result.ok(dto);
    }

    //用于兑换券的获取优惠券详情
    @ApiOperation("获取兑换券详情接口")
    @GetMapping(value = "/getCouponsDetailRedemption/{couponsNumber}")
    public Result<CouponsDetailRedemptionDTO> getCouponsDetailRedemption(@PathVariable String couponsNumber){
        StudentCoupons studentCoupons = couponsService.selectStudentCouponsByNumber(couponsNumber);
        Coupons coupons = couponsService.selectCouponsById(studentCoupons.getCouponsId());
        CouponsDetailRedemptionDTO dto = new CouponsDetailRedemptionDTO();
        dto.setCouponsId(coupons.getCouponsId());
        dto.setCouponsNumber(studentCoupons.getCouponsNumber());
        dto.setCouponsLabel(CouponsUtil.generateCouponLabel(coupons));
        dto.setStartDate(coupons.getStartDate());
        dto.setEndDate(coupons.getEndDate());
        dto.setStoreAddress(couponsService.selectAddressById(coupons.getAdvertiserId()));
        dto.setActivityDetails("1."+ coupons.getDescription() + "\n2." + coupons.getCondition());
        dto.setProductName(coupons.getProductName());
        dto.setProductImageUrl(SystemConstant.aliyunOSSUrlPrefix + coupons.getProductImageUrl());
        return Result.ok(dto);
    }

}
